package cn.tedu.xmfhlittlebird.controller;

import cn.tedu.xmfhlittlebird.pojo.dto.CouponAddNewDTO;
import cn.tedu.xmfhlittlebird.pojo.dto.CouponUpdateDTO;
import cn.tedu.xmfhlittlebird.pojo.vo.StoreCouponListItemVO;
import cn.tedu.xmfhlittlebird.pojo.vo.StoreCouponStandardVO;
import cn.tedu.xmfhlittlebird.service.ICouponService;
import cn.tedu.xmfhlittlebird.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/coupons")
@Slf4j
@Api(tags = "优惠券管理模块")
@Validated
public class CouponController {
    @Autowired
    private ICouponService couponService;

    @ApiOperation("添加优惠券")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Validated CouponAddNewDTO couponAddNewDTO) {
        log.debug("开始处理【添加优惠券】的请求：{}", couponAddNewDTO);
       couponService.addNew(couponAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("删除优惠券")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "优惠券id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id) {
        log.debug("开始处理【删除优惠券】的请求：id={}", id);
        couponService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改优惠券详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "优惠券id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable Long id, CouponUpdateDTO couponUpdateDTO) {
        log.debug("开始处理【修改优惠券详情】的请求：id={}, couponUpdateDTO={}", id, couponUpdateDTO);
        couponService.updateById(id, couponUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("启用优惠券")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "优惠券id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> setEnable(@PathVariable Long id) {
        log.debug("开始处理【启用优惠券】的请求：id={}", id);
        couponService.setEnable(id);
        return JsonResult.ok();
    }

    @ApiOperation("禁用优惠券")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParam(name = "id", value = "优惠券id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> setDisable(@PathVariable Long id) {
        log.debug("开始处理【禁用优惠券】的请求：id={}", id);
        couponService.setDisable(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据id查询优惠券详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "优惠券id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<StoreCouponStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理【根据id查询优惠券详情】的请求：id={}", id);
        StoreCouponStandardVO coupon = couponService.getStandardById(id);
        return JsonResult.ok(coupon);
    }

    @ApiOperation("查询优惠券列表")
    @ApiOperationSupport(order = 410)
    @GetMapping("")
    public JsonResult<List<StoreCouponListItemVO>> list() {
        log.debug("开始处理【查询优惠券列表】的请求……");
        List<StoreCouponListItemVO> list = couponService.list();
        return JsonResult.ok(list);
    }
    

}
